package com.kool.kmetrics.common.util;

import lombok.Getter;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Description 时间工具
 * @Author luyu
 * @Date 2019/4/16 13:40
 **/
public class DateUtil {
    /**
     * 取当月第一天
     *
     * @param year
     * @param month
     * @return
     */
    public static Date getFirstDateOfMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, 1, 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 取当月第一天
     *
     * @param month
     * @return
     */
    public static Date getFirstDateOfMonth(Date month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(month);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 取当月最后一天
     *
     * @param year
     * @param month
     * @return
     */
    public static Date getLastDateOfMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, 1);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    /**
     * 取当月最后一天
     *
     * @param month
     * @return
     */
    public static Date getLastDateOfMonth(Date month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(month);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    /**
     * 获取默认格式的时间字符串,显示当前时间
     *
     * @return
     */
    public static String showTime() {
        return dateString(new Date());
    }

    public static String dateString(Date date) {
        return dateString(date, DateFormatEnum.DEFAULT.format);
    }

    public static String dateString(Date date, String format) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

    /**
     * date之后minute分钟
     *
     * @param date
     * @param minute
     * @return
     */
    public static Date getMinuteAfter(Date date, int minute) {
        if (date == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minute);
        return calendar.getTime();
    }

    /**
     * date之后hour小时
     *
     * @param date
     * @param hour
     * @return
     */
    public static Date getHourAfter(Date date, int hour) {
        if (date == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, hour);
        return calendar.getTime();
    }

    /**
     * date之后day天
     *
     * @param date
     * @param day  小于0时往前算
     * @return
     */
    public static Date getDateAfter(Date date, int day) {
        if (date == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, day);
        return calendar.getTime();
    }

    /**
     * date之后month月
     *
     * @param date
     * @param month 小于0时往前算
     * @return
     */
    public static Date getMonthAfter(Date date, int month) {
        if (date == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, month);
        return calendar.getTime();
    }

    /**
     * 日期转为int类型的年月日（例如：20190506）
     *
     * @param date
     * @return
     */
    public static int toIntDay(Date date) {
        if (date == null) {
            return -1;
        }
        int day = 0;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateFormatEnum.YEAR_MONTH_DAY.format);
        String dateString = simpleDateFormat.format(date);
        day = Integer.parseInt(dateString);
        return day;
    }

    /**
     * 日期转为int类型的年月（例如：201905）
     *
     * @param date
     * @return
     */
    public static int toIntMonth(Date date) {
        if (date == null) {
            return -1;
        }
        int month = 0;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateFormatEnum.YEAR_MONTH.format);
        String dateString = simpleDateFormat.format(date);
        month = Integer.parseInt(dateString);
        return month;
    }

    /**
     * int类型年月日日期转为Date类型
     *
     * @param day
     * @return
     */
    public static Date toDateDay(int day) {
        int year = day / 10000;
        int month = (day % 10000) / 100;
        day = day % 100;
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, day, 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * int类型年月日期转为Date类型
     *
     * @param month
     * @return
     */
    public static Date toDateMonth(int month) {
        int year = month / 100;
        month = month % 100;
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, 1, 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }


    /**
     * 一天的开始时间点
     *
     * @param date
     * @return
     */
    public static Date getDateStart(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 一天的结束时间点
     *
     * @param date
     * @return
     */
    public static Date getDateEnd(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    /**
     * 输入时间字符串和响应格式，输出Date类型时间
     *
     * @param dateString
     * @param format
     * @return
     */
    public static Date parseDate(String dateString, DateFormatEnum format) throws ParseException {
        if (StringUtil.isEmpty(dateString)) {
            return null;
        }
        if (format == null) {
            format = DateFormatEnum.DEFAULT;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format.getFormat());
        ParsePosition pos = new ParsePosition(0);

        return simpleDateFormat.parse(dateString, pos);
    }

    public static Date parseDate(String dateStr, String format) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.parse(dateStr);
    }

    @Getter
    public enum DateFormatEnum {
        DEFAULT("yyyy-MM-dd HH:mm:ss.S", "年-月-日 时:分:秒.毫秒"),
        YMD_HMS("yyyy-MM-dd HH:mm:ss", "年-月-日 时:分:秒"),
        YEAR_MONTH("yyyyMM", "年月"),
        YEAR_MONTH_DAY("yyyyMMdd", "年月日"),
        YEAR_MONTH_DAY_SPLIT("yyyy-MM-dd", "年-月-日"),
        YMDHMS("yyyyMMddHHmmss", "年月日时分秒"),
        YEAR_MONTH_DAY_HMSS("yyyyMMddHHmmssSSS", "年月日时分秒毫秒");
        String format;
        String description;

        DateFormatEnum(String format, String description) {
            this.format = format;
            this.description = description;
        }
    }

    /**
     * 字符串转日期
     *
     * @param dateString
     * @return
     */
    public static Date parseDate(String dateString) throws ParseException {
        return parseDate(dateString, DateFormatEnum.YEAR_MONTH_DAY_SPLIT);
    }

    /**
     * 输入时间字符串(时分秒)输出Date格式时间
     *
     * @param time
     * @return
     */
    public static Date parseTime(String time) {
        return parseTime(new Date(), time);
    }

    /**
     * 输入时间字符串(时分秒)输出Date格式时间
     *
     * @param day
     * @param time
     * @return
     */
    public static Date parseTime(Date day, String time) {
        int hour = Integer.parseInt(time.substring(0, 2));
        int minute = Integer.parseInt(time.substring(3, 5));
        int second = Integer.parseInt(time.substring(6, 8));
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(day);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, second);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTime();
    }

}
